package main

import "fmt"

func main() {

	var a interface{}
	a = "hello"
	//fmt.Println(a)

	// 安全类型断言
	s, ok2 := a.(string)
	if ok2 {
		fmt.Println(s)
	}

	objIntNumber(100)
	objIntNumber("hello")
	objIntNumber(3.23)

	var u int32 = 88
	objIntNumber(u)

	fmt.Println(objType("hello")) // 字符串类型
	fmt.Println(objType(42))      // 整数
	fmt.Println(objType(true))    // 布尔值
	fmt.Println(objType(3.14))    // 浮点数
	fmt.Println(objType(nil))     // nil
}

func objIntNumber(any interface{}) {
	if n, ok := any.(int); ok {
		fmt.Println(n, "是整数")
	}
	fmt.Println(any, "不是整数")
}

func objType(v interface{}) string {
	switch v.(type) {
	case string:
		return "字符串类型"
	case int:
		return "整数"
	case int8, int16, int32, int64:
		return "有符号整数"
	case uint, uint8, uint16, uint32, uint64:
		return "无符号整数"
	case bool:
		return "布尔值"
	case float32, float64:
		return "浮点数"
	case complex64, complex128:
		return "复数"
	case nil:
		return "nil"
	default:
		return fmt.Sprintf("未知类型: %T", v)
	}
}
